<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

if (reqVar('azione') == 'elimina') {
	$msgPag .= confElim();
} 
$sqlSel = "SELECT * FROM rgl_utenti " .
		" WHERE id_utente = ".intval(reqVar('id'));
$objDB->query($sqlSel,'selRec');
if (!$acRec = $objDB->fetch('selRec')) {
	backOnErr("var. id. '".reqVar('id')."' mancante o errato");
}
$_REQUEST = array_merge($acRec,$_REQUEST);

$sql_sel = "SELECT r.cod_ruolo,r.descr" .
		" FROM rgl_utenti_ruoli ur " .
		" INNER JOIN rgl_ruoli r ON r.id_ruolo = ur.id_ruolo".
		" WHERE ur.id_utente = ".intval(reqVar('id')).
		" ORDER BY r.cod_ruolo ";
$objDB->query($sql_sel,'selRuo');

$sql_sel = "SELECT uo.cod_uorg,uo.descr" .
		" FROM rgl_utenti_uorg uuo " .
		" INNER JOIN rgl_uorg uo ON uo.id_uorg = uuo.id_uorg".
		" WHERE uuo.id_utente = ".intval(reqVar('id')).
		" ORDER BY uo.cod_uorg ";
$objDB->query($sql_sel,'selUorg');

$cmdL = "";
$cmdL .= "&nbsp;&nbsp;&nbsp;";
$sCmdPagR = '';
if (true) {
	$sCmdPagR .= "&nbsp;&nbsp;&nbsp;";
	$sCmdPagR .= "<a class='cmd1' href='utentiMod.php?id=".reqVar('id')."&$sXSNQS' >modifica</a>";
	if (true) {
		$sCmdPagR .= "&nbsp;&nbsp;&nbsp;";
		$sCmdPagR .= "<a class='cmd1' href='#' onclick='confElim();' >elimina</a>";
	}
}
$sCmdPagR .= cmdTorna(); 

testaHtml('Gestione applicazione', $msgPag, 'gest');
?>
<script language="JavaScript" type="text/javascript">
  	function confElim() {
  		if (confirm("Confermi l'eliminazione dell'utente ?")) {
  			document.location.href = "<?=$_SERVER['PHP_SELF']."?id=".reqVar('id')."&azione=elimina&".$sXSNQS?>";
  		}
  	}
</script>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left"><?=$cmdL?></td>
      <td class="cmd1" align="right"><?=$sCmdPagR?></td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
<?
$aCamForm = array();
require("_inc/const.campiUtente.php");
unset($aCamForm[40]);
echo getHtmlCampiForm($aCamForm,'vis');
?>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Ruoli</td>
		<td class='txt1' colspan='16'><?php 
while($acY = $objDB->fetch('selRuo')) {
            echo $acY['cod_ruolo']." - ".$acY['descr']."<br/>\n";
}
		?></td>
	</tr>
	<tr>
		<td class='lab1' colspan='4' valign='top'>Unita' organizzative</td>
		<td class='txt1' colspan='16'><?php 
while($acY = $objDB->fetch('selUorg')) {
            echo $acY['cod_uorg']." - ".$acY['descr']."<br/>\n";
}
		?></td>
	</tr>
</table>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right"><?=$sCmdPagR?></td>
   </tr>
</table>
<?
codaHtml();
exit();

function confElim() {
	global $objDB;
	$msg = '';
	$sSqlDel = "SELECT id_utente FROM rgl_bilancio_log WHERE id_utente = ".intval(reqvar('id'));
	$objDB->exec($sSqlDel);
	if ($acX=$objDB->fetch()) {
		$msg .= "esistono bilanci per questo utente<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSqlDel = "DELETE FROM rgl_parametri WHERE id_utente = ".intval(reqvar('id'));
	$objDB->exec($sSqlDel);
	$sSqlDel = "DELETE FROM rgl_utenti_gruppi WHERE id_utente = ".intval(reqvar('id'));
	$objDB->exec($sSqlDel);
	$sSqlDel = "DELETE FROM rgl_utenti_ruoli WHERE id_utente = ".intval(reqvar('id'));
	$objDB->exec($sSqlDel);
	$sSqlDel = "DELETE FROM rgl_utenti_uorg WHERE id_utente = ".intval(reqvar('id'));
	$objDB->exec($sSqlDel);
	$sSqlDel = "DELETE FROM rgl_utenti WHERE id_utente = ".intval(reqvar('id'));
	$objDB->exec($sSqlDel);
	//return "<pre>".print_r($objDB,1)."</pre>";
	header("Location: ".urlTorna('utente eliminato'));
	exit();
}
